<template>
	<!--登录 -->
	<view class="page-content">
		<view class="page-top">
			<!-- 标题栏和状态栏占位符 -->
			<view class="titleNview-placing"></view>
			<w-tit leftIcon="arrowleft" rightText="保存" rightColor="#198FFF" rightSize="14" @clickLeft="back"
				@clickRight="toAdd" title="认证信息" fontweight="bold" color="#000" iconfont="28"></w-tit>
		</view>
		<view class="page-body">
			<view class="tip">提示：上传完整企业资质有助获取优惠！</view>
			<view class="box-row">
				<authen-upload :params="objdata"  @delFile="delFile" :type="type" :required="true" @select="select" @progress="progress"
					@success="success" @fail="fail">
				</authen-upload>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapGetters,
		mapActions
	} from 'vuex'
	import {
		getOssUploadsParams,
		addCompanyAuths,companyAuthInfos
	} from '@/common/api.js';
	import store from '@/store'
	import wTit from '@/components/w-tit/w-tit-back.vue';
	import authenUpload from '@/pages/components/authentication_upload'
	export default {
		components: {
			wTit,
			authenUpload
		},
		data() {
			return {
				objdata: {
					company_id: '',
					company_name: '',
					user_name: '',
					code: '',
					mobile: '',
					industry: '',
					img: '',
				},
				id: '', //传参
				auth: '', //传参
				type:'', //传参
			}
		},
		computed: {
			...mapGetters(['companyInfo']),
		},
		onLoad(option) {
			console.log(222, option)
			if (option.id) {
				this.id = option.id;
			}
			if (option.auth) {
				this.auth = option.auth;
			}
			if(option.type){
				this.type = option.type;
				if(option.type==='edit'){
					this.getdata();
				}
			}
		},
		/* onNavigationBarButtonTap(e) {
			console.log(23,e)
			if (e.index == 0) {
			}
		}, */
		methods: {
			...mapActions('user', ['setToken']),
			delFile(){
				this.objdata.img = '';
			},
			tobrand() {
				uni.navigateTo({
					url: '/pages/brandCreation/brandList'
				})
			},
			//保存
			toAdd() {
				if (!!this.companyInfo) {
					this.objdata.company_id = this.companyInfo.id
				}
				let resdata = this.objdata;
				console.log(44, this.objdata)
				if (resdata.company_name === "") {
					uni.showToast({
						title: "企业名称不能为空！",
						icon: 'none',
						duration: 2000
					})
					return;
				}
				if (resdata.user_name === "") {
					uni.showToast({
						title: "法人名称不能为空！",
						icon: 'none',
						duration: 2000
					})
					return;
				}
				if (resdata.mobile === "") {
					uni.showToast({
						title: "电话不能为空！",
						icon: 'none',
						duration: 2000
					})
					return;
				}
				if (!/(^1[3|4|5|7|8][0-9]{9}$)/.test(resdata.mobile)) {
					uni.showToast({
						title: "请输入正确的手机号码！",
						icon: 'none',
						duration: 2000
					})
					return;
				}
				if (resdata.code === "") {
					uni.showToast({
						title: "统一信用代码不能为空！",
						icon: 'none',
						duration: 2000
					})
					return;
				}
				if (resdata.industry === "") {
					uni.showToast({
						title: "行业不能为空！",
						icon: 'none',
						duration: 2000
					})
					return;
				}
				if (resdata.img === "") {
					uni.showToast({
						title: "相关证件不能为空！",
						icon: 'none',
						duration: 2000
					})
					return;
				}
				addCompanyAuths(resdata).then(res => {
					console.log(12, res)
					if (res.code === 2000) {
						uni.navigateBack({
							delta: 1
						})
						setTimeout(() => {
						    uni.showToast({
						    	title:'保存成功！',
						    	icon:'none',
						    	duration:2000
						    })
						}, 100)
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none',
							duration: 2000
						})
					}
				})
			},
			// 获取上传状态
			select(e) {
				console.log('选择文件：', e)
				let _self = this;
				let filename = e.tempFiles[0]
				let file_path = e.tempFilePaths[0]
				let suffix = "." + filename.extname;
				let file_name = new Date().getTime() + Math.random().toString(36).substring(3, 20) + suffix
				console.log(312, file_name, filename)
				getOssUploadsParams().then(res => {
					console.log(123, res)
					let uploadTask = uni.uploadFile({
						url: res.data.host,
						filePath: file_path,
						fileType: 'image',
						name: 'file',
						formData: {
							name: file_path,
							'key': res.data.dir + file_name,
							'policy': res.data.policy,
							'OSSAccessKeyId': res.data.accessid,
							'success_action_status': '200',
							//让服务端返回200,不然，默认会返回204
							'signature': res.data.signature,
						},
						success: (res2) => {
							console.log('uploadImage success, res is:', res2)
							_self.objdata.img = res.data.host + '/office/' + file_name;
							uni.showToast({
								title: '上传成功',
								icon: 'success',
								duration: 1000
							});
						},
						fail: (err) => {
							console.log('uploadImage fail', err);
							uni.showModal({
								content: err.errMsg,
								showCancel: false
							});
						}
					});
					uploadTask.onProgressUpdate((res) => {
						console.log("上传进度" + res.progress);
						this.progres = res.progress;
					});
				})
			},
			// 获取上传进度
			progress(e) {
				console.log('上传进度：', e)
			},
			// 上传成功
			success(e) {
				console.log('上传成功')
			},
			// 上传失败
			fail(e) {
				console.log('上传失败：', e)
			},
			//获取oss签名信息
			getdata() {
				companyAuthInfos({company_id:this.id}).then(res => {
					if (res.code === 2000) {
						this.objdata = res.data;
					}
				})
			},
			back() {
				uni.navigateBack({
					delta: 1
				})
			},
		}
	}
</script>

<style lang="scss">
	.titleNview-placing {
		height: var(--status-bar-height);
		box-sizing: content-box;
		background: $uni-bg-color;
		display: block;
		position: sticky;
		top: 0rpx;
		z-index: 9999;
		overflow: hidden
	}

	.det-con-more {
		display: flex;
		justify-content: center;
		align-items: center;
		margin-bottom: 10rpx;
		color: #bbb;
		font-size: 24rpx;
	}

	.icon-tb {
		width: 32rpx;
		height: 32rpx;
		margin-right: 20rpx;
	}

	.page-content {
		display: flex;
		flex-direction: column;
		background: $uni-bg-color;
		width: 100vw;
		height: 100%;

		.page-top {
			display: flex;
			flex-direction: column;
			box-sizing: content-box;
			background: $uni-bg-color;
			display: block;
			position: sticky;
			top: 0rpx;
			z-index: 9999;
		}

		.page-body {
			display: flex;
			flex-direction: column;
			height: calc(100% - 90rpx - var(--status-bar-height));

			.tip {
				height: 80rpx;
				background: $uni-bg-color-grey;
				font-size: 28rpx;
				font-weight: 400;
				line-height: 40rpx;
				color: $uni-text-color;
				padding: 0 30rpx;
				line-height: 80rpx;
			}

			.box-row {
				margin-bottom: 180rpx;
				padding: 0 30rpx;
			}

			.btn-row {
				width: 100%;
				padding: 0 30rpx;

				button {
					line-height: 100rpx;
					background: $uni-but-bg-color;
					font-size: 36rpx;
					color: $font-color-w;
					height: 100rpx;
					box-shadow: 0rpx 0rpx 10rpx rgba(0, 0, 0, 0.19);
					border-radius: 10rpx;
				}
			}
		}
	}
</style>
